const MAX_SPEED = 127;

// Initializes the controls (gauge and slider) of a throttle page and defines
// functions that allow updating the train state, direction, and speed.
var throttle = null;

function initThrottlePage(emergency) {
	function updateGaugeSpeed() {
		var speed = 0;
		if (!throttle.emergency)
			speed = (throttle.isStopped ? 0 : throttle.speed);
		else
			speed = throttle.speed;
		gaugeData.setValue(0, 0, speed);
		gauge.draw(gaugeData, gaugeOptions);
	}
	
	function putDirection(direction) {
		throttle.direction = direction;
		if (direction == DIRECTION_FORWARD)
			$('#btnDirection').val("Forward");
		else if (direction == DIRECTION_BACKWARD)
			$('#btnDirection').val("Backward");
	}
	
	function putToggleSetting(buttonId, settingName, settingText, newValue) {
		throttle[settingName] = newValue;
		if (newValue == STATE_ON)
			$('#' + buttonId).val(settingText + ": On");
		else if (newValue == STATE_OFF)
			$('#' + buttonId).val(settingText + ": Off");
		else
			$('#' + buttonId).val(settingText + ": ?");
	}
	
	function putTrainState(state) {
		throttle.isStopped = false;
		var text = state.toString();
		
		switch (state) {
		case TRAIN_STATE_MOVING:
			text = "Moving";
			break;
		case TRAIN_STATE_WAITING:
			text = "Waiting";
			throttle.isStopped = true;
			break;
		case TRAIN_STATE_HALTED:
			text = "Halted";
			throttle.isStopped = true;
			break;
		case TRAIN_STATE_ERROR:
			text = "Error";
			throttle.isStopped = true;
			break;
		case TRAIN_STATE_BEGIN_CHANGE_DIRECTION:
			text = "Changing direction";
			break;
		case TRAIN_STATE_BEGIN_WAITING:
			text = "Beginning to wait";
			break;
		case TRAIN_STATE_BEGIN_HALTED:
			text = "Halting";
			break;
		}
		
		if (!throttle.emergency)
			$('#spanTrainState').html('State: ' + text);
		updateGaugeSpeed();
	}
	
	// Handle messages from the server to this open throttle
	function handleMessage(obj) {
		switch (obj.id) {
		case PUT_TRAIN_INFO:
			throttle.speed = obj.speed;
			updateGaugeSpeed();
			putDirection(obj.direction);
			putToggleSetting("btnLight", "light", "Light", obj.light);
			putToggleSetting("btnBell", "bell", "Bell", obj.bell);
			putToggleSetting("btnMute", "mute", "Mute", obj.mute);
			break;
		
		case PUT_TRAIN_STATE:
			putTrainState(obj.state);
			break;
		
		default:
			break;
		}
	}
	
	var trainId = getParameterByName("train_id");
	throttle = new Throttle(trainId, handleMessage, emergency);
	
	var gaugeData = new google.visualization.DataTable();
	gaugeData.addColumn('number', 'Speed');
	gaugeData.addRows(1);
	gaugeData.setCell(0, 0, 0);
	var gaugeOptions = {height: 160, width: 160, min: 0, max: MAX_SPEED, yellowFrom: 70, yellowTo: 100,
		redFrom: 100, redTo: MAX_SPEED, minorTicks: 10, 'animation.easing':'inAndOut'};
	var gauge;
	function drawGauge() {
		gauge = new google.visualization.Gauge(document.getElementById('gauge_div'));
		gauge.draw(gaugeData, gaugeOptions);
		
		$(function() {
			$("#slider").slider({
				create: function(event, ui) {
					$("#slider").slider( {value: 0, max: MAX_SPEED} );
				},
				
				slide: function(event, ui) {
					$("#speed").html(ui.value);
				},
				
				change: function(event, ui) {
					$("#speed").html(ui.value);
					throttle.setSpeed(ui.value);
				}
			});
		});
	}
	
	google.setOnLoadCallback(drawGauge);
}
